package brad.zimmerman.dungeon.game;

import java.util.HashMap;

import android.content.Context;
import android.graphics.PointF;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

public class ClassSoundObjects { 
    private static final String TAG = "SoundPoolSoundManager"; 

    public static final int SOUNDPOOL_STREAMS = 10; 

    private boolean enabled = true;
    private float volume = 1f;
    private Context context; 
    private SoundPool soundPool; 
    private HashMap<Integer, Integer> soundPoolMap; 
    private PointF location, lastPlayedCoords;

    public ClassSoundObjects(Context context) { 
            this.context = context;
            location = new PointF(0,0);
            lastPlayedCoords = new PointF(0,0);
    } 

    public void reInit() { 
            init(); 
    } 

    public boolean returnLastPlayedLocationSame(){
    	//Log.d(TAG, location.x + "," + lastPlayedCoords.x + "," + location.y + "," + lastPlayedCoords.y); 
    	return (location.x == lastPlayedCoords.x && location.y == lastPlayedCoords.y);
    }
    
    public void setLocation(PointF location){
    	this.location = location;
    }
    public void init() { 
            if (enabled) { 
                    //Log.d(TAG, "Initializing new SoundPool"); 
                    //re-init sound pool to work around bugs 
                    release(); 
                    soundPool = new SoundPool(SOUNDPOOL_STREAMS, AudioManager.STREAM_MUSIC, 100); 
                    soundPoolMap = new HashMap<Integer, Integer>(); 
                    soundPoolMap.put(1, soundPool.load(context, R.raw.fx_spell1, 1)); 
                    soundPoolMap.put(2, soundPool.load(context, R.raw.fx_spell2, 1)); 
                    soundPoolMap.put(3, soundPool.load(context, R.raw.fx_spell3, 1)); 
                    soundPoolMap.put(4, soundPool.load(context, R.raw.fx_splat, 1)); 
                    soundPoolMap.put(5, soundPool.load(context, R.raw.fx_step1, 1)); 
                    soundPoolMap.put(6, soundPool.load(context, R.raw.fx_step2, 1)); 
                    soundPoolMap.put(7, soundPool.load(context, R.raw.fx_coin_pickup, 1)); 
                    soundPoolMap.put(8, soundPool.load(context, R.raw.fx_power_up, 1)); 
                    soundPoolMap.put(9, soundPool.load(context, R.raw.fx_sword_clang, 1)); 
                    soundPoolMap.put(10, soundPool.load(context, R.raw.fx_potion_taken, 1)); 
                    soundPoolMap.put(11, soundPool.load(context, R.raw.fx_potion_found, 1)); 
                    soundPoolMap.put(12, soundPool.load(context, R.raw.fx_monsterdeath, 1)); 
                    soundPoolMap.put(13, soundPool.load(context, R.raw.fx_holy_chant, 1)); 
                    soundPoolMap.put(14, soundPool.load(context, R.raw.fx_door_open, 1)); 
                    soundPoolMap.put(15, soundPool.load(context, R.raw.fx_spell4, 1)); 
                    soundPoolMap.put(16, soundPool.load(context, R.raw.fx_glyphpickup, 1)); 
                    //Log.d(TAG, "SoundPool initialized"); 
            } 
    } 

    public void release() { 
            if (soundPool != null) { 
                    //Log.d(TAG, "Closing SoundPool"); 
                    soundPool.release(); 
                    soundPool = null; 
                    //Log.d(TAG, "SoundPool closed"); 
                    return; 
            } 
    } 

    public void playSound(int sound) { 
            if (soundPool != null && enabled) { 
                    //Log.d(TAG, "Playing Sound " + sound); 
                    //AudioManager mgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
                    //int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); 
                    Integer soundId = soundPoolMap.get(sound); 
                    if (soundId != null) { 
                            soundPool.play(soundPoolMap.get(sound), volume, volume, 0, 0, 1f); 
                    } 
                    lastPlayedCoords = location;
            } 
    } 
    
    public void setEnabled(boolean enabled) { 
            this.enabled = enabled; 
    } 
    public void turnOff(){
    	enabled = false;
    }
    public void setVolume(float level){
    	volume = (level/100);
    }
    public float getVolume(){
		return volume * 100;
	}
    
} 